@extends('admin/common')

@section('content')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i>
        {{ $now_nav[0]['name'] }} <span class="c-gray en">&gt;</span>
        {{ $now_nav[1]['name'] }}
        <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
    </nav>
    <div class="page-container">
        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <span class="l">
                <a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
                <a href="javascript:;" onclick="admin_add('添加管理员','{{ URL::to('admin/admin/addAdmin') }}','800','500')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加管理员</a>
            </span>
        </div>
        <table class="table table-border table-bordered table-bg" id="adminTable">
            <thead>
            <tr>
                <th><input type="checkbox" name="" value=""></th>
                <th>ID</th>
                <th>用户名</th>
                <th>昵称</th>
                <th>头像</th>
                <th>邮箱</th>
                <th>加入时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
    </div>
    @endsection

@section('script')
    <script type="text/javascript" src="{{ asset('admin/lib/My97DatePicker/4.8/WdatePicker.js') }}"></script>
    <script type="text/javascript" src="{{ asset('admin/lib/datatables/1.10.0/jquery.dataTables.min.js') }}"></script>
    <script type="text/javascript" src="{{ asset('admin/lib/laypage/1.2/laypage.js') }}"></script>
    <script type="text/javascript" src="{{ asset('admin/dataTables.fz.js') }}"></script>
    <script type="text/javascript">
        $(function () {
            //封装databtales函数 ,参数1:为表单对象, 2:为config配置对象, 3:数据操作对象
            var config = {
                "searching": true,    //搜索
                "ordering" : true,
                //排序
                "order": [
                    [1, 'desc'],
                    [6, 'desc']
                ]
            };
            var ajaxOp = {
                "ajax": {
                    "url" : "{{ URL::to('admin/admin/indexPost') }}",
                    "dataType" : 'json',
                    "type" : "POST",
                    "headers" : {
                        'X-CSRF-TOKEN': " {{ csrf_token() }}"
                    }
                },
                //数据项
                "aoColumns": [
                    { "data": "id" ,"bSortable": false},
                    { "data": "id" , "orderData":[ 1 ]},
                    { "data": "name", "bSortable": false },
                    { "data": "nickname", "bSortable": false },
                    { "data": "avatar", "bSortable": false },
                    { "data": "email", "bSortable": false },
                    { "data": "created_at" },
                    { "data": "id", "bSortable": false }
                ],
                //自定义数据项
                'columnDefs' : [
                    {
                        "render" : function(data, type, row){
                            return '<input type="checkbox" value="'+data+'" name="ids">';
                        },
                        "targets" : 0
                    },
                    {
                        "render" : function(data, type, row){
                            var html = '<a title="编辑" href="javascript:;" onclick="admin_edit(\'管理员编辑\','+data+',\'800\',\'500\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> ';
                            if(data != 1){
                                html += '<a title="删除" href="javascript:;" onclick="admin_del(this,'+data+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>'
                            }
                            return html;
                        },
                        "targets" : 7
                    }
                ]
            };
            dataTbalesInit($('#adminTable'),config,ajaxOp);
        });

        /*
         参数解释：
         title	标题
         url		请求的url
         id		需要操作的数据id
         w		弹出层宽度（缺省调默认值）
         h		弹出层高度（缺省调默认值）
         */
        /*管理员-增加*/
        function admin_add(title,url,w,h){
            layer_show(title,url,w,h);
        }
        /*管理员-删除*/
        function admin_del(obj,id){
            layer.confirm('确认要删除吗？',function(index){
                $.ajax({
                    type: 'POST',
                    url: '{{ URL::to('admin/admin/delAdmin') }}',
                    data : {
                        id : id
                    },
                    dataType: 'json',
                    success: function(ret){
                        if(ret.code == 1){
                            $(obj).parents("tr").remove();
                            layer.msg('已删除!',{icon:1,time:1000});
                        }else{
                            layer.msg('删除失败!',{icon:1,time:1000});
                        }
                    },
                    error:function(data) {
                        console.log(data.msg);
                    }
                });
            });
        }
        /*管理员-编辑*/
        function admin_edit(title,id,w,h){
            var url = '{{ URL::to('admin/admin/editAdmin') }}'+'/'+id;
            layer_show(title,url,w,h);
        }

        //批量删除
        function datadel(){
            var ids  = [];
            $('input[name="ids"]:checked').each(function (index , item) {
                 if($(item).val() != 1){
                     ids.push($(item).val());
                 }
            });
            layer.confirm('确认要删除吗？',function(index){
                $.ajax({
                    type: 'POST',
                    url: '{{ URL::to('admin/admin/delAdmin') }}',
                    data : {
                        id : ids.join()
                    },
                    dataType: 'json',
                    success: function(ret){
                        if(ret.code == 1){
                            layer.msg('已删除!',{icon:1,time:1000}, function(){
                                document.location.reload();
                            });
                        }else{
                            layer.msg('删除失败!',{icon:1,time:1000});
                        }
                    },
                    error:function(data) {
                        console.log(data.msg);
                    }
                });
            });
        }
    </script>
@endsection